package com.manman.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manman.commonUtils.Result;
import com.manman.eduService.entity.EduTeacher;
import com.manman.eduService.entity.vo.TeacherQuery;
import com.manman.eduService.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-16
 */
@Api(description = "讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/eduService/teacher")
public class EduTeacherController {

    @Autowired
    EduTeacherService eduTeacherService;

    // 1. 查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result getAllTeacher(){
        // 调用 service 方法实现查询所有
        List<EduTeacher> list = eduTeacherService.list(null);
        if (list == null){
            return Result.error().data("items", null);
        }
        return Result.ok().data("items", list);
    }

    // 2. 逻辑删除讲师
    @ApiOperation(value = "根据 ID 逻辑删除讲师")
    @DeleteMapping("/{id}")
    public Result removeTeacher(
            @ApiParam(name="id", value = "讲师 ID", required = true)
            @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if(b){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 3. 分页查询讲师
     * current 当前页
     * limit 每页记录数
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/pageTeacher/{current}/{limit}")
    @ApiOperation("分页查询讲师")
    public Result pageListTeacher(@PathVariable long current, @PathVariable long limit){
        // 创建 page 对象
        Page<EduTeacher> pageTeacher = new Page<EduTeacher>(current, limit);
        // 调用方法实现分页，底层封装，把所有分页素有数据封装到 page 里
        eduTeacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();      // 总记录数
        List<EduTeacher> list = pageTeacher.getRecords();      // 数据 list 集合
        return Result.ok().data("total", total).data("items", list);
    }

    /**
     * 多条件分页查询
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation("多条件分页查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                       @RequestBody(required = false) TeacherQuery teacherQuery){
        System.out.println(teacherQuery);
        // 创建 page 对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询，判断条件之是否为空，不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create", end);
        }

        // 降序排列
        wrapper.orderByDesc("gmt_modified");
        // 调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> list = pageTeacher.getRecords();
        return Result.ok().data("total", total).data("items", list);
    }

    /**
     * 添加讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 根据讲师 ID 查询讲师
     * @param id
     * @return
     */
    @ApiOperation("根据讲师 ID 查询讲师")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return Result.ok().data("teacher", teacher);
    }

    /**
     * 讲师修改
     * @param eduTeacher
     * @return
     */
    @ApiOperation("讲师修改")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if (update){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}

